Thread: [beginner] float and division

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    1

    [beginner] float and division

    hi this is my first post

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main() {
     
     int a,b;
     
     printf("Insert 2 numbers:\n");
     scanf("%d%d",&a,&b);
    
     printf("Result is: %f \n",a/b); 
     
     system("pause");
     return 0;
    }
    the result isn't correct i always obtain 0.00 also if, for example, i write 8 and 2 as scanned number. why?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Try printing a and b to see that you're actually getting numbers. Also, integer division will always yield integer results.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    May 2007
    Posts
    147
    cast a/b to floats with something like

    (float) a / (float) b;

    That provides a float result.

    As it is, you're probably sending an integer when printf is expecting a float, which might easily produce nonsense.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by JVene View Post
    which might easily produce nonsense.
    Change "might" into "will" - floating point numbers are quite different from integers in their internal components. So there is no chance that a floating point number will make much sense as an integer or the other way around. It'll look as nice as monkeys typing on a calculator.

    It would actually make sense to use a cast to double rather than float, since printf doesn't ever take a float argument - all floating point values will convert to double before printf gets to see the value. And you only need to cast one side of the operator, so:
    Code:
    printf("%f\n", (double)a / b);
    should give the right result.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    May 2007
    Posts
    147
    Ah, true - doubles....yish, I should probably stay in the C++ forum, my C is rusty.

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by matsp View Post
    Change "might" into "will" - floating point numbers are quite different from integers in their internal components. So there is no chance that a floating point number will make much sense as an integer or the other way around. It'll look as nice as monkeys typing on a calculator.
    NITPICK: 0 can be represented the same way a integers and floating point. (And usually is, though sometimes -0 is used, which is equal the minimum integer value instead.)
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. float number division
    By hoistyler in forum C Programming
    Replies: 6
    Last Post: 01-14-2009, 03:13 AM
  2. issues in float division a/b
    By George2 in forum C# Programming
    Replies: 17
    Last Post: 04-24-2008, 06:15 AM
  3. int division to float
    By rimig88 in forum C++ Programming
    Replies: 4
    Last Post: 04-22-2008, 08:48 AM
  4. Help W/ Functions!!
    By LiquidR6 in forum C Programming
    Replies: 2
    Last Post: 02-15-2005, 05:17 PM
  5. Backdooring Instantaneous Radius of Curvature & Functions
    By just2peachy in forum C++ Programming
    Replies: 8
    Last Post: 10-06-2004, 12:25 PM